import { createApp } from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import Cookies from 'js-cookie'
import 'element-plus/dist/index.css'
import 'nprogress/nprogress.css'

require('@/utlis/FriendlyTime.js');

router.beforeEach((to, from, next) => {
	if (Cookies.get("token") || to.path == '/Login' || to.path == '/mistake') {
		next()
	} else {
		ElMessage.warning("请先登录")
		next({ path: '/Login', query: { lo: to.path } })
	}
})
let app = createApp(App);
app.use(store).use(router)

app.mixin({
	methods: {
		// 初始化对象
		inits(obj, defaultValue) {
			Object.keys(obj).forEach((key) => {
				if (!obj[key]) return;
				let typeName = obj[key].constructor.name;
				switch (typeName) {
					case Number.name:
						obj[key] = 0;
						break;
					case Array.name:
						obj[key] = [];
						break;
					case Boolean.name:
						obj[key] = false;
						break;
					default:
						obj[key] = undefined;
						break;
				}
			});
			Object.assign(obj, defaultValue);
		}
	}
})
//自定义指令
app.directive("permission", {
	mounted(el, binding) {
		let val = binding.value;
		var permissionList = store.getters.permissionList || []
		if (permissionList.indexOf(val) === -1) {
			el.remove();
		}
	}
})
app.mount('#app')


